<?php

namespace app\api\service;

use think\facade\Db;

class SpikeGoodsService
{
    /**
     * 获取首页秒杀商品
     * */
    public static function getHomeSpikeGoodsList($store_id)
    {
        $spike_session_id = Db::name('spike_session')
                                ->alias('a')
                                ->join('spike_meeting b','a.spike_meeting_id=b.id')
                                ->where(['b.store_id' => $store_id,'b.status' =>1])
                                ->whereBetweenTimeField('a.start_time','a.end_time')
                                ->value('a.id');

        if (empty($spike_session_id)){
            return  [];
        }

        $spikeGoodsList = static::getSpikeGoodsList($spike_session_id,true);

        return $spikeGoodsList;
    }


    /**
     * 根据场次获取秒杀商品
     * */
    public static function getSpikeGoodsList($spike_session_id,$simple = false)
    {

         $query = Db::name('goods')
                            ->field('b.id,a.goods_name,a.main_image,a.sell_price,b.spike_price,b.total_sale')
                            ->alias('a')
                            ->join('spike_goods b','a.id=b.goods_id')
                            ->join('spike_session_goods c','b.id=c.spike_goods_id')
                            ->where('c.spike_session_id',$spike_session_id)
                            ->withAttr('main_image',function ($value){
                                return request()->domain() . $value;
                            })->order(['b.sort' => 'asc','b.total_sale' => 'desc','b.id' => 'desc']);

        if($simple){
            $spikeGoodsList = $query->limit(10)->select();
            return $spikeGoodsList;
        }

        $spikeGoodsList = $query->paginate(10);

        return $spikeGoodsList;
    }

    /**
     * 获取秒杀商品详情
     *
     * */

    public static function findSpikeGoods($id)
    {
        $spikeGoods = Db::name('goods')
                        ->field('b.id,a.goods_name,a.main_image,a.spec_type,a.goods_images,a.goods_attr,a.sell_price,b.spike_price,
                        b.spike_stock,b.total_sale,b.status,d.content,c.spike_session_id')
                        ->alias('a')
                        ->join('spike_goods b','a.id=b.goods_id')
                        ->join('spike_session_goods c','b.id=c.spike_goods_id')
                        ->join('goods_content d','a.id=d.goods_id')
                        ->where('b.id',$id)
                        ->withAttr('main_image',function ($value){
                            return request()->domain() . $value;
                        })->withAttr('goods_attr',function ($value){
                            if(empty($value)){
                                return [];
                            }
                            return json_decode($value,true);
                        })->withAttr('goods_images',function ($value){
                            $imgs = [];
                            $arr = json_decode($value,true);
                            foreach ($arr as $v){
                                $imgs[] = request()->domain() . $v;
                            }
                            return $imgs;
                        })->find();

        $items = [];
        if ($spikeGoods['spec_type']==2){
            $items = Db::name('goods_item')
                        ->alias('a')
                        ->field('b.id,a.attr,a.image,a.sell_price,b.spike_price,b.spike_stock')
                        ->join('spike_goods_item b','a.id=b.sku_id')
                        ->withAttr('image',function ($value){
                            return request()->domain() . $value;
                        })->where('b.spike_goods_id',$spikeGoods['id'])
                        ->select();
        }

        $spikeGoods['items'] = $items;


        return $spikeGoods;

    }
}